//
//  ZTHistoryViewController.m
//  XMLYFM
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "ZTHistoryViewController.h"
#import "ZTHistoryTableViewCell.h"
#import "ZTFavorModel.h"
#import "ZHPlayViewController.h"

#define WIDTH [[UIScreen mainScreen] bounds].size.width

#define HEIGHT [[UIScreen mainScreen] bounds].size.height

@interface ZTHistoryViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *historyTableView;

@property (nonatomic, strong) NSMutableArray *historyArray;

@end

@implementation ZTHistoryViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.historyArray = [NSMutableArray arrayWithArray:[[ZHFMDB shareFMDB] selectTrackData]];
    [self.historyTableView reloadData];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    
#pragma mark - 创建tableView
    self.historyTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    [self.view addSubview:self.historyTableView];
    self.historyTableView.backgroundColor = [UIColor whiteColor];
    
    self.historyTableView.dataSource = self;
    self.historyTableView.delegate = self;
    
    self.historyTableView.rowHeight = WIDTH * 0.24;
    
    [self.historyTableView registerClass:[ZTHistoryTableViewCell class] forCellReuseIdentifier:@"historyTableCell"];
    self.historyTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    [self.historyTableView reloadData];
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return HEIGHT * 0.06;
}

#pragma mark 设置tableview区数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.historyArray.count;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZTHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"historyTableCell"];
    
    ZTAlbumModel *model = [self.historyArray objectAtIndex:indexPath.row];
    
    cell.model = model;
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return WIDTH*0.28;
}


#pragma mark - 点击跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ZTAlbumModel *model = self.historyArray[indexPath.row];
    ZHPlayViewController *player = [ZHPlayViewController sharaMusicPlayer];
    ZHNavigationBarController *naVC = [[ZHNavigationBarController alloc] initWithRootViewController:player];
    
    player.allMusicArray = _historyArray;
    player.song_id = model.trackId;
    player.indexRow = indexPath.row;
    
    [self presentViewController:naVC animated:YES completion:^{
        
    }];
    
}



-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
        UIButton *headBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        headBtn.frame = CGRectMake(0, 0, WIDTH, HEIGHT * 0.06);
    
        [headBtn setTitle:@"清空历史记录" forState:UIControlStateNormal];
        [headBtn setTitleColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1] forState:UIControlStateNormal];
    
        [headBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        
        return headBtn;
}


-(void)btnAction:(UIButton *)btn
{
   
    [[ZHFMDB shareFMDB] dropTableTracks];
    self.historyArray = [NSMutableArray array];
    
    [self.historyTableView reloadData];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
